window.onload = function () {
    const update = Vue.createApp({
        data(){
            return {
                newName:null,
                newAddress:null,
                newPwd:null,
                isFail:false
            }
        },
        created: function () {
            const xhr = new XMLHttpRequest();
            xhr.open("GET", "http://localhost:8080/GetInfoServlet");
            xhr.send();
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status >= 200 && xhr.status < 300) {
                        if(xhr.response === "") window.location.href="login.html";
                        let user = JSON.parse(xhr.response);
                        update.newName = user['name'];
                        update.newAddress = user['address'];
                        update.newPwd = user['pwd'];
                    }
                }
            }
        },
        methods:{
            submitUpdate(){
                const xhr = new XMLHttpRequest();
                url = "http://localhost:8080/UpdateUserInfoServlet?"
                url = url + "name=" + update.newName + "&address=" + update.newAddress + "&pwd=" + update.newPwd;
                xhr.open("POST", url);
                xhr.send();
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === 4) {
                        if (xhr.status >= 200 && xhr.status < 300) {
                            if(xhr.response === "true"){
                                alert("修改成功");
                                update.isFail = false;
                                window.location.href = "index.html"

                            }
                            else{
                                console.log("修改失败");
                                update.isFail = true;
                            }
                        }
                    }
                }
            },
        },
    }).mount("#app");
}